package com.wj.crm.controller;

import com.wj.crm.base.BaseController;
import com.wj.crm.base.ResultInfo;
import com.wj.crm.query.DataDicQuery;
import com.wj.crm.query.UserQuery;
import com.wj.crm.service.DataDicService;
import com.wj.crm.vo.DataDic;
import com.wj.crm.vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author WangJin
 * @version 1.0
 * @date 2022/2/9 21:36
 */
@Controller
@RequestMapping("data_dic")
public class DataDicController extends BaseController {

    @Autowired
    private DataDicService dataDicService;


    /**
     * 多多条件查询所有的字典数据
     * @return
     */
    @RequestMapping("list")
    @ResponseBody
    public Map<String,Object> selectByParams(DataDicQuery userQuery){

        return dataDicService.selectByParams(userQuery);
    }


    @RequestMapping("index")
    public String index(){
        return "dataDic/data_dic";
    }

    @PostMapping("add")
    @ResponseBody
    public ResultInfo dicAdd(DataDic datadic){
        dataDicService.addDic(datadic);
        return success("字典添加成功!");
    }

    @RequestMapping("toAddOrUpdateDicPage")
    public String toAddOrUpdateDicPage(Integer id, HttpServletRequest request){
        //判断id是否为空，不为空表示更新操作，查询用户对象
        if(id != null){
            //通过id查询对象
            DataDic dataDic = dataDicService.selectByPrimaryKey(id);
            request.setAttribute("dataDic",dataDic);
        }
        return "dataDic/add_update";
    }

    @PostMapping("update")
    @ResponseBody
    public ResultInfo userUpdate(DataDic datadic){
        dataDicService.updateDic(datadic);
        return success("字典更新成功!");
    }

    @PostMapping("delete")
    @ResponseBody
    public ResultInfo delete(Integer[] ids){
        dataDicService.deleteByIds(ids);
        return success("字典删除成功！");
    }
}
